<?php

namespace App\ApiFrontend\User;

use App\Http\Controllers\ApiBaseController;
use App\Models\UserModel;
use Illuminate\Support\Facades\Hash;


/**
 *  添加或编辑
 */
class Update extends ApiBaseController
{

  /**
   * 参数检查
   */
  protected function check(): void
  {
    try {
      request()->validate([
        'userId' => 'required|int',
        'phone' => 'required',
      ]);
    } catch (\Exception $exception) {
      $this->result["code"] = 1;
      $this->result["message"] = "参数错误: " . $exception->getMessage();
    }
  }

  /**
   * 业务主体
   */
  protected function service(): void
  {
    //数据处理
    $userId = request('userId');
    try {
      $user = UserModel::find($userId);
      if (empty($user)) {
        throw new \Exception("用户不存在");
      }
      $user->phone = request('phone');
      $user->save();
    } catch (\Exception $exception) {
      $this->result["code"] = 1;
      $this->result["message"] = $exception->getMessage();
    }
    return;
  }
}
